package com.lu.model.exception;

import cn.hutool.core.util.StrUtil;

/**
 * @Description 系统断言
 * @Author: zhanglu
 * @Date: 2022/7/26 16:12
 */
public class LuBootAssert {

  public static void isTrue(boolean expression, String errorMsgTemplate, Object... params) throws RuntimeException {
    if (!expression) {
      throw new LuBootException(StrUtil.format(errorMsgTemplate, params));
    }
  }

  public static void isFalse(boolean expression, String errorMsgTemplate, Object... params) throws RuntimeException {
    if (expression) {
      throw new LuBootException(StrUtil.format(errorMsgTemplate, params));
    }
  }

  public static <T extends RuntimeException> void isTrue(Class<T> t, boolean expression, String errorMsgTemplate, Object... params) throws RuntimeException {
    if (!expression) {
      T rs = null;
      try {
        rs = t.getDeclaredConstructor(String.class).newInstance(StrUtil.format(errorMsgTemplate, params));
      } catch (Exception e) { }
      throw rs;
    }
  }

  public static <T extends RuntimeException> void isFalse(Class<T> t, boolean expression, String errorMsgTemplate, Object... params) throws RuntimeException {
    if (expression) {
      T rs = null;
      try {
        rs = t.getDeclaredConstructor(String.class).newInstance(StrUtil.format(errorMsgTemplate, params));
      } catch (Exception e) { }
      throw rs;
    }
  }

}
